# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html

import scrapy

from components.custom_item import UpdateCustomItem
from utils.tools import *


class NetPatentProItem(UpdateCustomItem):
    __update_key__ = [
        "patent_name",
        "patent_type",
        "patent_status",
        "pub_number",
        "pub_date",
        "grant_number",
        "grant_date",
        "patent_num",
        "application_time",
        "applicant_name",
        "inventor",
        "address",
        "post_code",
        "cat",
        "agent",
        "agency",
        "priority_info",
        "title",
        "abstracts",
        "img_url",
        "uni",
        "all_cat_num",
    ]
    __table_name__ = "net_patent_pro"

    patent_name = scrapy.Field()
    patent_type = scrapy.Field()
    patent_status = scrapy.Field()
    pub_number = scrapy.Field()
    pub_date = scrapy.Field()
    grant_number = scrapy.Field()
    grant_date = scrapy.Field()
    patent_num = scrapy.Field()
    app_number = scrapy.Field()
    application_time = scrapy.Field()
    applicant_name = scrapy.Field()
    inventor = scrapy.Field()
    address = scrapy.Field()
    post_code = scrapy.Field()
    cat = scrapy.Field()
    agent = scrapy.Field()
    agency = scrapy.Field()
    priority_info = scrapy.Field()
    title = scrapy.Field()
    abstracts = scrapy.Field()
    img_url = scrapy.Field()
    uni = scrapy.Field()
    all_cat_num = scrapy.Field()
    main_cat_num = scrapy.Field()

    def pre_to_db(self):
        """
        入库前的处理
        """
        self.pub_date = self.__format_date__(self.pub_date)
        self.grant_date = self.__format_date__(self.grant_date)
        self.application_time = self.__format_date__(self.application_time)
        if isinstance(self.abstracts, str):
            self.abstracts = {"content": self.abstracts}

    @staticmethod
    def __format_date__(text):
        date_str = None
        if text:
            try:
                date_str = str(datetime.datetime.strptime(text, "%Y.%m.%d").date())
            except ValueError:
                date_str = str(datetime.datetime.strptime(text, "%Y%m%d").date())
        return date_str


class NetPatentProUpdatePatentStatusItem(NetPatentProItem):
    __update_key__ = [
        "patent_status",
    ]
    __table_name__ = "net_patent_pro"


class NetPatentLawStatusItem(UpdateCustomItem):
    __update_key__ = ["status", "date", "uni"]

    uni = scrapy.Field()
    date = scrapy.Field()
    app_number = scrapy.Field()
    status = scrapy.Field()
    detail = scrapy.Field()


class NetPatentLawStatusDetailItem(UpdateCustomItem):
    __update_key__ = ["status", "date", "uni", "detail"]
    __table_name__ = "net_patent_law_status"

    uni = scrapy.Field()
    date = scrapy.Field()
    app_number = scrapy.Field()
    status = scrapy.Field()
    detail = scrapy.Field()

    def pre_to_db(self):
        self.date = self.__format_date__(self.date)

    @staticmethod
    def __format_date__(text):
        date_str = None
        if text:
            date_str = str(datetime.datetime.strptime(text, "%Y.%m.%d").date())
        return date_str


class NetPatentProRowsItem(NetPatentProItem):
    __update_key__ = [
        "patent_name",
        "patent_type",
        "patent_status",
        "pub_number",
        "pub_date",
        "grant_number",
        "grant_date",
        "patent_num",
        "application_time",
        "applicant_name_one",
        "applicant_name",
        "inventor",
        "address",
        "post_code",
        "cat",
        "agent",
        "agency",
        "priority_info",
        "title",
        "abstracts",
        "img_url",
        "uni",
        "all_cat_num",
    ]
    applicant_name_one = scrapy.Field()
    __table_name__ = "net_patent_pro_rows"


class NetPatentProRowsUpdatePatentStatusItem(NetPatentProUpdatePatentStatusItem):
    applicant_name_one = scrapy.Field()
    __table_name__ = "net_patent_pro_rows"


class NetPatentSimilarItem(UpdateCustomItem):
    __update_key__ = [
        "pid",
        "similar_publication_id",
        "similar_papplication_id",
    ]
    pid = scrapy.Field()
    similar_publication_id = scrapy.Field()
    similar_papplication_id = scrapy.Field()


class NetPatentCitedItem(UpdateCustomItem):
    __update_key__ = [
        "pid",
        "cited_publication_id",
        "cited_application_id",
        "cited_correlation",
    ]
    pid = scrapy.Field()
    cited_publication_id = scrapy.Field()
    cited_application_id = scrapy.Field()
    cited_correlation = scrapy.Field()


class NetPatentCiteItem(UpdateCustomItem):
    __update_key__ = [
        "pid",
        "cite_publication_id",
        "cite_application_id",
        "cite_correlation",
    ]
    pid = scrapy.Field()
    cite_publication_id = scrapy.Field()
    cite_application_id = scrapy.Field()
    cite_correlation = scrapy.Field()
